//
//  YJCRelateLogin.m
//  xiangzhe
//
//  Created by lyt on 16/11/23.
//  Copyright © 2016年 com.dazhebao. All rights reserved.
//

#import "YJCRelateLogin.h"
#import "YTTimeButton.h"
#import "YJCIsMobile.h"

@interface YJCRelateLogin ()
@property (nonatomic, strong) UITextField *phoneNum;
@property (nonatomic, strong) UITextField *safeCode;
@property (nonatomic, strong) YTTimeButton *sendBtn;
@end

@implementation YJCRelateLogin

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_denglu"]];
    if (!self.userName) {
        [self.navigationController popViewControllerAnimated:NO];
    }
    
    [self setupUI];
}
- (void)sendMessage{
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
    //获取到手机号 验证有效性 发送网络请求
    if ([YJCIsMobile stringIsMobile:self.phoneNum.text]) {
        self.sendBtn.userInteractionEnabled = YES;
        [self.sendBtn beginTime:60 title:@"重新发送" sendingColor:[UIColor lightGrayColor] finishColor:[UIColor lightGrayColor]];
        
        [[YJCNetWork sharedNetWork] requestWithMethod:POST serviceName:@"User.SendMobileCode" parameters:@{@"phone":self.phoneNum.text} finshed:^(id result, NSError *error) {
            NSLog(@"%@",result);
            
        }];
        
    }else{
        [SVProgressHUD showImage:nil status:@"请输入正确的手机号"];
        [SVProgressHUD setOffsetFromCenter:UIOffsetMake(0, KScreenH / 4)];
        
    }
    
}

- (void)loginBtnClick{
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
    if (self.phoneNum.text.length == 11 && self.safeCode.text.length > 0) {
        [SVProgressHUD showWithStatus:@"正在登录"];
        
        [[YJCNetWork sharedNetWork] requestWithMethod:POST serviceName:@"User.Quick_login" parameters:@{@"phone":self.phoneNum.text,@"code":self.safeCode.text} finshed:^(id result, NSError *error) {
            NSLog(@"%@---%@",result,error);

            NSString *resultCode = result[@"data"][@"resultCode"];

            if (resultCode.intValue == 0) {
                [SVProgressHUD dismiss];
                [self dismissViewControllerAnimated:YES completion:^{
                }];
                
            }else{
                NSString *mg = result[@"data"][@"resultInfo"];
                NSLog(@"网络错误 %@",mg);
                [SVProgressHUD showImage:nil status:mg];
            }
        }];
    }else{
        [SVProgressHUD showImage:nil status:@"手机号或者验证码错误"];
        [SVProgressHUD setOffsetFromCenter:UIOffsetMake(0, KScreenH / 4)];
    }
}

- (void)setupUI{
    
    //标题
    UILabel *titleLab = [[UILabel alloc] init];
    titleLab.text = @"关联账号";
    titleLab.font = [UIFont systemFontOfSize:17];
    titleLab.textColor = [UIColor yt_colorWithHexString:@"#685d5d"];
    titleLab.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:titleLab];
    
    // 分割线
    UIView *topView = [[UIView alloc] init];
    topView.backgroundColor = [UIColor yt_colorWithHexString:@"#efefef"];
    [self.view addSubview:topView];
    
    // 头像
    UIImageView *headView = [[UIImageView alloc] init];
    headView.bounds = CGRectMake(0, 0, 50, 50);
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.userIcon]];
    if (data) {
        headView.image = [UIImage imageWithData:data];
    }else{
        headView.image = [UIImage imageNamed:@"def_photo"];
    }
    headView.layer.cornerRadius = 25;
    headView.layer.masksToBounds = YES;
    [self.view addSubview:headView];
    // 提示语
    UILabel *tishiLab = [[UILabel alloc] init];
    tishiLab.textAlignment = NSTextAlignmentLeft;
    tishiLab.numberOfLines = 0;
    tishiLab.text = [NSString stringWithFormat:@"亲爱的%@\n为了您的账号安全请关联您的手机号",self.userName];
    tishiLab.textColor = [UIColor yt_colorWithHexString:@"#685d5d"];
    tishiLab.font = [UIFont systemFontOfSize:12];
    [self.view addSubview:tishiLab];
    // 手机号
    UITextField *phoneNum = [[UITextField alloc] init];
    phoneNum.textColor = [UIColor yt_colorWithHexString:@"#685d5d"];
    phoneNum.placeholder = @"请输入手机号";
    phoneNum.font = [UIFont systemFontOfSize:13];
    
    phoneNum.layer.borderColor = [UIColor yt_colorWithHexString:@"#efefef"].CGColor;
    phoneNum.layer.borderWidth = 1.0;
    phoneNum.layer.cornerRadius = 3.0;
    phoneNum.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 0)];
    phoneNum.leftViewMode = UITextFieldViewModeAlways;
    [self.view addSubview:phoneNum];
    self.phoneNum = phoneNum;
    phoneNum.tintColor = [UIColor blueColor];
    phoneNum.keyboardType = UIKeyboardTypePhonePad;
    // 验证码
    UITextField *safeCode = [[UITextField alloc] init];
    safeCode.textColor = [UIColor yt_colorWithHexString:@"#685d5d"];
    safeCode.placeholder = @"验证码";
    safeCode.font = [UIFont systemFontOfSize:13];
    
    safeCode.layer.borderColor = [UIColor yt_colorWithHexString:@"#efefef"].CGColor;
    safeCode.layer.borderWidth = 1.0;
    safeCode.layer.cornerRadius = 3.0;
    safeCode.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 0)];
    safeCode.leftViewMode = UITextFieldViewModeAlways;
    [self.view addSubview:safeCode];
    self.safeCode = safeCode;
    
    //发送验证码的按钮
    YTTimeButton *timeBtn = [YTTimeButton buttonWithType:UIButtonTypeCustom];
    [timeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [timeBtn setTitleColor:[UIColor yt_colorWithHexString:@"#685d5d"] forState:UIControlStateNormal];
    timeBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    timeBtn.layer.borderColor = [UIColor yt_colorWithHexString:@"#ff3366"].CGColor;
    timeBtn.layer.borderWidth = 1.0;
    timeBtn.layer.cornerRadius = 3.0;
    [self.view addSubview:timeBtn];
    [timeBtn addTarget:self action:@selector(sendMessage) forControlEvents:UIControlEventTouchUpInside];
    self.sendBtn = timeBtn;

    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [loginBtn setTitle:@"绑定并登录" forState: UIControlStateNormal];
    [loginBtn setBackgroundColor:[UIColor yt_colorWithHexString:@"#ff3366"]];
    loginBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.view addSubview:loginBtn];
    loginBtn.alpha = 0.45;
    [loginBtn addTarget:self action:@selector(loginBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(20);
        make.centerX.equalTo(self.view);
    }];
    
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(60);
        make.height.mas_equalTo(1);
        make.width.mas_equalTo(KScreenW);
    }];
    
    [headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(kHdistance);
        make.top.equalTo(topView).offset(kHdistance);
//        make.height.with.mas_equalTo(50);
    }];
    
    [tishiLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(headView.mas_right).offset(kVdistance);
        make.centerY.equalTo(headView);
    }];
    
    [phoneNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(kHdistance);
        make.top.equalTo(headView.mas_bottom).offset(kHdistance);
        make.height.mas_equalTo(30);
    }];
    
    [timeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(-kHdistance);
        make.centerY.equalTo(phoneNum);
        make.left.equalTo(phoneNum.mas_right).offset(kVdistance);
        make.width.mas_equalTo(80);
    }];
    
    [safeCode mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(phoneNum.mas_bottom).offset(2*kVdistance);
        make.left.equalTo(self.view).offset(kHdistance);
        make.right.equalTo(self.view).offset(-kHdistance);
        make.height.mas_equalTo(30);
    }];
    
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(safeCode.mas_bottom).offset(2*kVdistance);
        make.left.equalTo(self.view).offset(kHdistance);
        make.right.equalTo(self.view).offset(-kHdistance);
    }];

}
@end
